package com.utonw.carloan.adpter.applyloan;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.utonw.carloan.R;
import com.utonw.carloan.bean.applyloan.LoanPlanBean;

import java.util.List;

/**
 * Created by SEELE on 2017/3/29.
 */

public class ShowRepayPlanAdapter extends BaseExpandableListAdapter {

    private List<LoanPlanBean.ResultBean.RepayPlanListBean> groupArray;
    private List<LoanPlanBean.ResultBean.RepayPlanListBean.RepayPlanDetailForAppBean> childArray;
    private Context mContext;

    public ShowRepayPlanAdapter(Context context,
                                List<LoanPlanBean.ResultBean.RepayPlanListBean> groupArray,
                                List<LoanPlanBean.ResultBean.RepayPlanListBean.RepayPlanDetailForAppBean> childArray) {
        mContext = context;
        this.groupArray = groupArray;
        this.childArray = childArray;
    }

    @Override
    public int getGroupCount() {
        return null != groupArray && 0 != groupArray.size() ? groupArray.size() : 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupArray.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childArray.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = convertView;
        GroupHolder holder = null;
        if (view == null) {
            holder = new GroupHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_expandlist_show_repay_plan_group, null);
            holder.remark = (TextView) view.findViewById(R.id.tv_count_numbs);
            holder.repayTotal = (TextView) view.findViewById(R.id.tv_total);
            holder.arrow = (ImageView) view.findViewById(R.id.iv_arrow);
            view.setTag(holder);
        } else {
            holder = (GroupHolder) view.getTag();
        }

        //判断是否已经打开列表
        if (isExpanded) {
            holder.arrow.setImageResource(R.mipmap.body_up);
        } else {
            holder.arrow.setImageResource(R.mipmap.body_down);
        }

        // TODO: 2017/4/1 设置数据源
        holder.remark.setText(groupArray.get(groupPosition).getPeriod());
        holder.repayTotal.setText(groupArray.get(groupPosition).getTotalMoney());

        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = convertView;
        ChildHolder holder = null;
        if (view == null) {
            holder = new ChildHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_expandlist_show_repay_plan_item, null);
            holder.repayCapital = (TextView) view.findViewById(R.id.tv_hkbj);
            holder.repayInterest = (TextView) view.findViewById(R.id.tv_hklx);
            holder.repayDate = (TextView) view.findViewById(R.id.tv_hkrq);
            holder.depreciationFee = (TextView) view.findViewById(R.id.tv_zhfwf);
            holder.gpsCost = (TextView) view.findViewById(R.id.tv_blf);
            holder.drunkDrive = (TextView) view.findViewById(R.id.tv_gpsllf);
            holder.parkCost = (TextView) view.findViewById(R.id.tv_tcf);
            holder.cashdepositFee = (TextView) view.findViewById(R.id.tv_fxbzj);
            view.setTag(holder);
        } else {
            holder = (ChildHolder) view.getTag();
        }

        // TODO: 2017/4/1 赋值
        holder.repayCapital.setText(childArray.get(childPosition).getRepayCapital());
        holder.repayInterest.setText(childArray.get(childPosition).getRepayInterest());
        holder.repayDate.setText(childArray.get(childPosition).getRepayDate());
        holder.depreciationFee.setText(childArray.get(childPosition).getDepreciationFee());
        holder.gpsCost.setText(childArray.get(childPosition).getGpsCost());
        holder.drunkDrive.setText(childArray.get(childPosition).getDrunkDrive());
        holder.parkCost.setText(childArray.get(childPosition).getParkCost());
        holder.cashdepositFee.setText(childArray.get(childPosition).getCashdepositFee());

        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class GroupHolder {
        //第X期
        TextView remark;
        //还款总额
        TextView repayTotal;
        //箭头
        ImageView arrow;

    }

    private class ChildHolder {
        //还款本金
        TextView repayCapital;
        //还款利息
        TextView repayInterest;
        //还款日期
        TextView repayDate;
        //综合服务费
        TextView depreciationFee;
        //办理费
        TextView gpsCost;
        //GPS流量费
        TextView drunkDrive;
        //停车费
        TextView parkCost;
        //风险保证金
        TextView cashdepositFee;

    }
}
